



var ajax = require("../ajax/ajax");

var yz = require("../comm/yz");

var mongo = require("../mongo/mongo");

var validator = require("validator");
var moment = require("moment");
const task = require('../dict/task');

var mongoose = require('mongoose');
const py = require('../comm/py');

var fFlow = require("../flow/fFlow");

const dcustomerOrder = require('../dict/customerOrder');
var math = require("mathjs");
const fcontract = require('../contract/fContract');
exports.getJd = function (info) {
    // body...




};


exports.getDataJd = function (item) {
    // body... 

    var nodeIdArray = [];

    item.nodeList.forEach(function (node) {
        nodeIdArray.push(node.array)
    }, this);

};

//开始一个订单的流程
exports.beginCustomerOrderFlow = async function (customerOrderId) {
    // body...
    if (!customerOrderId) {
        throw '需要订单编号!'
    }

    var customerOrder = await mongo.findOneAsync({
        data: { _id: customerOrderId },
        collection: "customerOrder"
    });


    if (customerOrder.openProof) {
        if (yz.isArray(customerOrder.flow.proof.nodeList)) {

            var rootNode = {};

            customerOrder.flow.proof.nodeList.forEach(function (node) {
                if (node.taskClassId == "root") {
                    rootNode = node
                    return false
                }
            }, this);
            if (rootNode.statusId == 0) {
                await fFlow.exeNode(customerOrderId, "proof", rootNode.id.toString());
            }
        }
    }


    if (customerOrder.openBulk) {
        if (yz.isArray(customerOrder.flow.bulk.nodeList)) {

            var rootNode = {};

            customerOrder.flow.bulk.nodeList.forEach(function (node) {
                if (node.taskClassId == "root") {
                    rootNode = node
                    return false
                }
            }, this);
            if (rootNode.statusId == 0) {
                await fFlow.exeNode(customerOrderId, "bulk", rootNode.id.toString());
            }
        }
    }


};

exports.count = async function (customerOrderId) {
    // body...

    var customerOrder = await mongo.findOneAsync({
        data: {
            _id: customerOrderId
        },
        collection: "customerOrder"

    });
    var detal = [];
    customerOrderDetail = dcustomerOrder.fmtDetalType(customerOrder);
    var amount = 0;
    var reAmount = 0;
    customerOrderDetail.forEach(function (item) {
        amount = math.chain(item.amount).add(amount).round(2).value;
        reAmount = math.chain(item.reAmount).add(reAmount).round(2).value;
    }, this);


    var statusId = 0;
    if (customerOrderDetail.length > 0) {
        if (amount < reAmount) {
            //金额小于收款金额

            statusId = 300

        }
        else if (amount > reAmount) {
            statusId = 200

        }
        else {
            statusId = 300
        }

    }

    await mongo.upAsync({
        updateFind: {
            _id: mongoose.Types.ObjectId(customerOrderId)

        }, updateSet: {
            $set: {
                statusId: statusId,
                count: {
                    amount: amount,
                    reAmount: reAmount
                }
            }

        },
        collection: "customerOrder"
    })

    customerOrder.contractInfo = yz.IsNullThen(customerOrder.contractInfo, {})

    await fcontract.count(customerOrder.contractInfo._id)

    // if (customerorder.proof)



};




exports.countData = function (item) {
    // body...





};